home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1995 March / SOFM_Mar1995.bin / mac / SRI / General Interest / Programming / J / Examples / files.js < prev    next >
Encoding:
Text File  |  1993-12-02  |  2.8 KB  |  138 lines  |  [TEXT/????]

  1.    NB. native (DOS) file read/write utilities
  2.    NB.
  3.    NB. 'string' verbs write out text delimited by CRLF,
  4.    NB. and read in text delimited by LF.
  5.    NB.
  6.    NB. read verbs take an optional left argument:
  7.    NB.     a single number is:   length of read
  8.    NB.     two numbers are:      index of read, length of read
  9.    NB.
  10.    NB. file write verbs return number of characters written
  11.    NB.
  12.    NB.   dat nappend file    append
  13.    NB.   dat nappends file   append string
  14.    NB.       nexist file     return 1 if file exists
  15.    NB.   opt nmread file     read file as matrix
  16.    NB.   opt nread file      read file
  17.    NB.   opt nreads file     read string
  18.    NB.   opt nrread file     read records
  19.    NB.   opt nvread file     read file as boxed vector
  20.    NB.   dat nwrite file     write file
  21.    NB.   dat nwrites file    write string
  22.    
  23.    t=. 'nappend nappends nexist nmread nread nreads nrread'
  24.    t=. t,' nvread nwrite nwrites'
  25.    SCRIPTNAMES=: t
  26.    
  27.    NB. nappend
  28.    nappend=: 0 : 0
  29. :
  30. r=. '0' ". 'x. 1!:3 <>y.'
  31. >(#r){(#x.);'write error'
  32. )
  33.    
  34.    NB. nappends
  35.    nappends=: 0 : 0
  36. :
  37. x=. ,x.,"1 CRLF
  38. $.=. (1<#$x.)}.$.
  39. x=. (_2*CRLF e.~_1{x.)}.toCRLF x
  40. r=. '0' ". 'x 1!:3 <>y.'
  41. >(#r){(#x);'write error'
  42. )
  43.    
  44.    NB. nexist
  45.    nexist=:    '0'&".@('>: $ 1!:11 '''&,)@(,&''';0 0')
  46.    
  47.    NB. nmread
  48.    NB. read records as matrix
  49.    NB. x. = optional index, length
  50.    nmread=: 0 : 0
  51. 1e9 nmread y.
  52. :
  53. s=. 1!:4 y =. <>y.
  54. 'bl'=. _2{.x.
  55. b=. (l<0){b,s+1
  56. l=. (s-b)<.|l
  57. d=. toLF 1!:11 y,<b,l
  58. d=. d -. 26{a.
  59. ] ;._2 d,LF #~ LF ~: {:d
  60. )
  61.    
  62.    NB. nread
  63.    NB. read file, x. = optional index, length
  64.    nread=: 0 : 0
  65. 1e9 nread y.
  66. :
  67. s=. 1!:4 y =. <>y.
  68. 'bl'=. _2{.x.
  69. b=. (l<0){b,s+l
  70. l=. (s-b)<.|l
  71. 1!:11 y,<b,l
  72. )
  73.    
  74.    NB. nreads
  75.    NB. read file as script, x. = index, length
  76.    nreads=: 0 : 0
  77. 1e9 nreads y.
  78. :
  79. s=. 1!:4 y =. <>y.
  80. 'bl'=. _2{.x.
  81. b=. (l<0){b,s+1
  82. l=. (s-b)<.|l
  83. d=. toLF 1!:11 y,<b,l
  84. d }.~ -+/(10 26{a.)e._2{.d
  85. )
  86.    
  87.    NB. nrread
  88.    NB. read records from flat file
  89.    NB. x. = optional record index, number of records
  90.    nrread=: 0 : 0
  91. 1e9 nrread y.
  92. :
  93. max=. 1!:4 y =. <>y.
  94. len=. 0 * blk=. 10000<.max
  95. dat=. 1!:11 y,<0,blk
  96. $.=. $.}.~blk>cls =. dat i.CR
  97. 'not organized in records' [ $.=.''
  98. len=. >:cls+LF=(cls+1){dat,' '
  99. max=. <.max%len
  100. 'bl'=. _2{.x.
  101. b=. (<:max) <. (l<0){b,max+l
  102. l=. (max-b)<.|l
  103. dat=. (l,len)$ 1!:11 y,<len * b,l
  104. dat=. (cls-len)}."1 dat
  105. )
  106.    
  107.    NB. nvread
  108.    NB. read records as boxed vector
  109.    NB. x. = optional index, length
  110.    nvread=: 0 : 0
  111. 1e9 nvread y.
  112. :
  113. s=. 1!:4 y =. <>y.
  114. 'bl'=. _2{.x.
  115. b=. (l<0){b,s+1
  116. l=. (s-b)<.|l
  117. d=. toLF 1!:11 y,<b,l
  118. d=. d }.~ -(26{a.) = {:d
  119. <;._2 d,LF #~ LF ~: {: d
  120. )
  121.    
  122.    NB. nwrite
  123.    nwrite=: 0 : 0
  124. :
  125. r=. '0' ". 'x. 1!:2 <>y.'
  126. >(#r){(#x.);'write error'
  127. )
  128.    
  129.    NB. nwrites
  130.    nwrites=: 0 : 0
  131. :
  132. x=. ,x.,"1 CRLF
  133. $.=. (1<#$x.)}.$.
  134. x=. (_2*CRLF e.~_1{x.)}. toCRLF x
  135. r=. '0' ". 'x 1!:2 <>y.'
  136. >(#r){(#x);'write error'
  137. )              
  138.